﻿using RFID.BLL;
using RFID.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RFID.PC.AppCenter
{
    public partial class AddOrderForm : RFID.PC.ChildForm
    {
        public AddOrderForm(MainForm main, object sender)
            : base(main, sender)
        {
            InitializeComponent();
            Text = Title;
        }

        private void AddOrderForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = GoodsInfoBLL.Select();
            Column1.DisplayIndex = 3;
        }

        private void TxtCard_DoubleClick(object sender, EventArgs e)
        {
            GetTagIdByDoubleClick(sender);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCard.Text))
            {
                ShowMessage("未识别一卡通账号！", false);
                return;
            }

            OrderInfo order = new OrderInfo()
            {
                Date = DateTime.Now,
                RId = txtCard.Text
            };
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                int count = Convert.ToInt32(row.Cells[0].Value);
                if (count > 0)
                {
                    int id = Convert.ToInt32(row.Cells[1].Value);
                    int price = Convert.ToInt32(row.Cells[3].Value);
                    order.OrderDetial.Add(new OrderDetial() { GId = id, Price = price, Count = count });
                }
            }
            if (order.OrderDetial.Count > 0)
            {
                var result = OrderInfoBLL.AddOrder(order);
                ShowResult(result);
            }
            else
            {
                ShowMessage("未指定消费产品！", false);
            }
        }
    }
}
